package com.itxx.keyCase.Demo4;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class TcpClient {
    public static void main(String[] args) throws IOException {
        //1.创建Socket对象

        Socket socket = new Socket("127.0.0.1",54321);

        //2.通过io流传输数据      创建字节输出流
        OutputStream os = socket.getOutputStream();
        //将字节输出流转换为字符输出流
        OutputStreamWriter osw = new OutputStreamWriter(os);
        BufferedWriter bw = new BufferedWriter(osw);
        String s = "这是通过Tcp协议传输的数据";

        bw.write(s);
        bw.flush();

        //3.释放资源
        bw.close();
        socket.close();

    }
}
